package org.xinghai.sql.main.controller;

import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xinghai.sql.common.api.Result;
import org.xinghai.sql.main.dto.CreateTableDto;
import org.xinghai.sql.main.generator.domain.ColumnElements;
import org.xinghai.sql.main.generator.domain.DatabaseTables;
import org.xinghai.sql.main.generator.domain.TableColumns;
import org.xinghai.sql.main.generator.service.DatabaseTablesService;

import java.util.List;


/**
 *数据库表的增删改查
 * @author lijia
 * &#064;date  2025/07/28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/database")
public class DatabaseController {

    @Autowired
    private DatabaseTablesService databaseTablesService;
    //创建表
    @RequestMapping("/createTable")
    public Result<String> createTable(
            @RequestBody CreateTableDto createTableDto
    ) {
        databaseTablesService.createTable(createTableDto);
        return Result.success("创建表成功");
    }
    //删除表
    @RequestMapping("/deleteTable")
    public Result<String> deleteTable(
            @RequestParam("tableId") Integer tableId
    ) {
        databaseTablesService.removeById(tableId);
        return Result.success("删除表成功");
    }
    //查询表
    @RequestMapping("/getTable")
    public Result<DatabaseTables> getTable(
            @RequestParam("tableId") Integer tableId
    ) {
        return Result.success(databaseTablesService.getById(tableId));
    }

    //更改表
    @RequestMapping("/updateTable")
    public Result<String> updateTable(
            @RequestBody DatabaseTables databaseTables
    ) {
        databaseTablesService.updateById(databaseTables);
        return Result.success("更改表成功");
    }
    //查询这个表的所有列
    @RequestMapping("/getAllColumns")
    public Result<List<TableColumns>> getAllColumns(
            @RequestParam("tableId") Integer tableId
    ) {
        List<TableColumns> result = databaseTablesService.getTableColumnsByTableId(tableId);
        return Result.success(result);
    }
    //通过所属数据库查询表
    @GetMapping("/getDataBaseByDatabaseName")
    public Result<List<DatabaseTables>> getDataBaseByDatabaseName(
            @RequestParam("databaseName") String databaseName
    ){
        return Result.success(databaseTablesService.getTableByDatabaseName(databaseName));
    }
}
